<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class management extends Admin_My_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('greybox');
        $this->load->model('general/general_model');
        $this->load->helper('indo_date');
    }

    public function data_karyawan() {
        $this->load->model('management/data_karyawan');

        $data = array();

        $field_name = array("a.nik", "a.nama", "b.divisi", "a.no_hp", "a.no_telepon", "a.email", "a.alamat");
        $field_title = array("NIK", "Nama Karyawan", "Divisi", "No. HP", "Telepon", "Email", "Alamat");
        $field_type = array("text", "text", "text", "text", "text", "text", "text");

        $filter_panel = new FilterPanel(base_url() . 'index.php/', $field_name, $field_title, $field_type);
        $whr = $filter_panel->getFilterString();

        $data['post'] = $this->getPostInfo();
        $this->exportLastQuery();

        $data['gb'] = $this->greybox->createLink(base_url() . 'index.php/management/karyawan_edit', '', 'Tambah Data', 'add');
        $data['fp'] = $filter_panel->drawfilterpanel();
        $data['fp2'] = $filter_panel->drawfilterpanelsingle();
                
        $data['resultSetAll'] = $this->data_karyawan->getDataAllKaryawan($whr);
        $data['resultSet'] = $this->data_karyawan->getDataKaryawan($whr, PAGES_LIMIT, $data['post']['offset']);
        $data['resultSetResume'] = $this->data_karyawan->getDataResumeKaryawan();
        $data['result'] = $data['resultSet']->result();
        $data['resultResume'] = $data['resultSetResume']->result();
        $data['numRows'] = $data['resultSet']->num_rows();
        $data['numRowsResume'] = $data['resultSetResume']->num_rows();

        $data['paging'] = $this->MakePagingPage($data['resultSetAll']['last_query'], $data['post']['offset'], $data['post']['colPage'], $data['post']['nextPage']);
        $_SESSION[USER_AUTH . 'last_query'] = $data['resultSetAll']['last_query'];

        $this->log_event = "Membuka menu data keryawan";

        $this->loadMenu('/management/data_karyawan', $data);
    }

    public function data_karyawan_delete($kode = "") {
        $this->load->model('management/data_karyawan');

        if ($kode != null) {
            $this->data_karyawan->removeDataKaryawan($kode);

            $this->log_event = "Menghapus Data Karyawan dengan Nik " . $kode;

            redirect(base_url() . 'index.php/management/data_karyawan', 'refresh');
        }
    }

    public function data_karyawan_detil($kode) {
        $this->load->model('/management/karyawan_detil');

        $data = array();

        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['kode'] = $kode;
        $data['msg'] = "";

        if ($data['kode'] != "") {
            $resultSet = $this->karyawan_detil->getDataKaryawanDetil($data['kode']);
            $row = $resultSet->row();

            $data['divisi_karyawan'] = $row->divisi_karyawan;
            $data['nik'] = $row->nik;
            $data['nama'] = $row->nama;
            $data['nama_panggilan'] = $row->nama_panggilan;
            $data['tempat_lahir'] = $row->tempat_lahir;
            $data['tanggal_lahir'] = $row->tanggal_lahir;
            $data['alamat'] = $row->alamat;
            $data['kota'] = $row->kota;
            $data['propinsi'] = $row->propinsi;
            $data['agama'] = $row->agama;
            $data['warga_negara'] = $row->warga_negara;
            $data['status_marital'] = $row->status_marital;
            $data['no_telepon'] = $row->no_telepon;
            $data['no_hp'] = $row->no_hp;
            $data['email'] = $row->email;
            $data['tanggal_masuk'] = $row->tanggal_masuk;
        }

        $this->log_event = "Melihat detil data karyawan dengan id karyawan $kode";

        $this->loadMenuHeaderPopup('/management/data_karyawan_detil', $data);
    }

    public function data_karyawan_edit($kode) {        
        $this->load->model('/management/karyawan_edit');
        $this->load->model('/management/karyawan_detil');
        $this->load->model('/management/data_karyawan');

        $data = array();

        $data['divisi_karyawan'] = $this->input->get_post('divisi_karyawan');
        $data['id_divisi'] = $this->input->get_post('id_divisi_karyawan');
        $data['nik'] = $this->input->get_post('nik');
        $data['nama'] = $this->input->get_post('nama');
        $data['nama_panggilan'] = trim($this->input->get_post('nama_panggilan'));
        $data['tempat_lahir'] = $this->input->get_post('tempat_lahir');
        $data['tanggal_lahir'] = $this->input->get_post('tanggal_lahir');
        $data['alamat'] = $this->input->get_post('alamat');
        $data['kota'] = $this->input->get_post('kota');
        $data['propinsi'] = $this->input->get_post('propinsi');
        $data['agama'] = $this->input->get_post('agama');
        $data['warga_negara'] = $this->input->get_post('warga_negara');
        $data['status_marital'] = $this->input->get_post('status_marital');
        $data['no_telepon'] = $this->input->get_post('no_telepon');
        $data['no_hp'] = $this->input->get_post('no_hp');
        $data['email'] = $this->input->get_post('email');
        $data['tanggal_masuk'] = $this->input->get_post('tanggal_masuk');
        $data['jabatan'] = $this->input->get_post('jabatan');
        $data['norek'] = $this->input->get_post('norek');
        $data['norek_bank'] = $this->input->get_post('norek_bank');
        $data['norek_an'] = $this->input->get_post('norek_an');
        if ($this->input->get_post('reset_keluar')== "OK"){
            $data['tanggal_keluar'] = "NULL";
        } else {
            $data['tanggal_keluar'] = $this->input->get_post('tanggal_keluar');
        }
        $data['id_group_vessel'] = $this->input->get_post('id_group_vessel');

        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['kode'] = $kode;
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['nik'] == "") {
                $data['msg'] = "Nik harus diisi!";
            } else if ($data['nama'] == "") {
                $data['msg'] = "Nama harus diset !";
            } else if ($data['tempat_lahir'] == "") {
                $data['msg'] = "Tempat lahir harus diisi !";
            } else if ($data['tanggal_lahir'] == "") {
                $data['msg'] = "Tanggal Lahir harus diisi !";
            } else if ($data['tanggal_masuk'] == "") {
                $data['msg'] = "Tanggal masuk harus diisi !";
            } else {
                $arrVals = array(
                    "id_divisi" => $data['id_divisi'],
                    "nik" => $data['nik'],
                    "nama" => $data['nama'],
                    "nama_panggilan" => $data['nama_panggilan'],
                    "tempat_lahir" => $data['tempat_lahir'],
                    "tanggal_lahir" => $data['tanggal_lahir'],
                    "alamat" => $data['alamat'],
                    "kota" => $data['kota'],
                    "propinsi" => $data['propinsi'],
                    "agama" => $data['agama'],
                    "warga_negara" => $data['warga_negara'],
                    "status_marital" => $data['status_marital'],
                    "no_telepon" => $data['no_telepon'],
                    "no_hp" => $data['no_hp'],
                    "email" => $data['email'],
                    "tanggal_masuk" => $data['tanggal_masuk'],
                    "tanggal_keluar" => $data['tanggal_keluar'],
                    "jabatan" => $data['jabatan'],
                    "norek" => $data['norek'],
                    "norek_bank" => $data['norek_bank'],
                    "norek_an" => $data['norek_an'],
                    "id_group_vessel" => $data['id_group_vessel']
                );

                if ($data['vMode'] == "add") {
                    $this->karyawan_edit->addDataKaryawan($data['nik'], $arrVals);

                    $this->log_event = "Menambah data karyawan dengan nik $data[nik]";

                    echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
                } else {
                    $this->karyawan_edit->updateDataKaryawan($data['kode'], $arrVals);

                    $this->log_event = "Merubah data karyawan dengan nik $data[kode]";

                    echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
                }
            }
        } else {
            if ($data['kode'] != "") {                
                $resultSet = $this->karyawan_detil->getDataKaryawanDetil($data['kode']);                                                
                
                $row = $resultSet->row();           
                
                $data['divisi_karyawan'] = $row->divisi_karyawan;
                $data['id_divisi_karyawan'] = $row->id_divisi;
                $data['nik'] = $row->nik;
                $data['nama'] = $row->nama;
                $data['nama_panggilan'] = $row->nama_panggilan;
                $data['tempat_lahir'] = $row->tempat_lahir;
                $data['tanggal_lahir'] = $row->tanggal_lahir;
                $data['alamat'] = $row->alamat;
                $data['kota'] = $row->kota;
                $data['propinsi'] = $row->propinsi;
                $data['agama'] = $row->agama;
                $data['warga_negara'] = $row->warga_negara;
                $data['status_marital'] = $row->status_marital;
                $data['no_telepon'] = $row->no_telepon;
                $data['no_hp'] = $row->no_hp;
                $data['email'] = $row->email;
                $data['tanggal_masuk'] = $row->tanggal_masuk;
                $data['tanggal_keluar'] = $row->tanggal_keluar;
                $data['id_group_vessel'] = $row->id_group_vessel;
                $data['nama_vessel'] = $row->nama_vessel;
                $data['jabatan'] = $row->jabatan;
                $data['norek'] = $row->norek;
                $data['norek_bank'] = $row->norek_bank;
                $data['norek_an'] = $row->norek_an;                
            }
        }

        $data['gbdivisi'] = $this->greybox->createLink(base_url() . 'index.php/popup/divisi_karyawan', '<span>Cari</span>', 'Cari Divisi', 'find', '', 750, 450);
        
        $data['data_kapal'] = $this->karyawan_detil->getDataKapal();
        $this->loadMenuHeaderPopup('/management/data_karyawan_edit', $data);
    }

    public function user_password_edit($kode) {
        $this->load->model('management/password_user_model');

        if (urldecode($kode) != $this->userauth->getUserID())
            die("Anda tidak memiliki akses untuk mengedit password dari " . urldecode($kode));

        $data = array();

        $data['kode'] = str_replace("%20", " ", $kode);
        $data['id_user'] = $this->input->get_post('id_user');
        $data['nik'] = $this->input->get_post('nik');
        $data['nama'] = $this->input->get_post('nama');
        $data['password'] = $this->input->get_post('status');
        $data['id_group_user'] = $this->input->get_post('id_group_user');
        $data['group_user'] = $this->input->get_post('group_user');
        $data['passworddb'] = $this->input->get_post('passworddb');
        $data['passwordlama'] = $this->input->get_post('passwordlama');
        $data['passwordbaru'] = $this->input->get_post('passwordbaru');
        $data['cpasswordbaru'] = $this->input->get_post('cpasswordbaru');
        $encryptpasswordlama = md5($data['passwordlama']);
        $encryptpasswordbaru = md5($data['passwordbaru']);
        $vSave = $this->input->get_post('vSave');

        if ($vSave) {
            if ($data['id_user'] == "") {
                $data['msg'] = "Parameter ID Pengguna tidak ditemukan!";
            } else if ($data['passwordlama'] == "") {
                $data['msg'] = "Parameter Password Lama tidak ditemukan!";
            } else if ($encryptpasswordlama != $data['passworddb']) {
                $data['msg'] = "password lama tidak cocok, periksa kembali password lama anda !";
            } else if ($data['passwordbaru'] == "") {
                $data['msg'] = "Parameter Password Baru tidak ditemukan!";
            } else if ($data['cpasswordbaru'] != $data['passwordbaru']) {
                $data['msg'] = "Confirm Password Baru tidak sama!";
             } else if ($data['cpasswordbaru'] == "") {
                $data['msg'] = "Parameter Confirm Password Baru tidak ditemukan!";
            } else if ($encryptpasswordlama == $encryptpasswordbaru) {
                $data['msg'] = "Parameter Password Baru dan password lama sama!";
            } else if ($data['nik'] == "") {
                $data['msg'] = "Parameter NIK tidak ditemukan!";
            } else {
                $this->log_event = "Mengubah Password User Pengguna " . $data['id_user'];

                $arrVals = array(
                    "password" => $encryptpasswordbaru
                );

                $this->password_user_model->editpassworduser($data['id_user'], $arrVals);

                $data["sender"] = $this->password_user_model->get_scalar("select nilai from global_setting where kunci='id_modul_interface_textbased'", "nilai");

                $data["no_hp"] = $this->password_user_model->get_scalar("
                    SELECT mt_karyawan.nama, mt_karyawan.no_hp
                    FROM mt_user
                    LEFT JOIN mt_karyawan ON mt_karyawan.nik = mt_user.nik
                    LEFT JOIN mt_group_user ON mt_group_user.id_group_user = mt_user.id_group_user WHERE mt_user.id_user = '$data[id_user]'", "no_hp"
                );


                $this->log_event = "Merubah password $kode";

                echo "<script>alert('Password anda telah berhasil diubah. Silakan login dengan password yang baru.');</script>";
                echo "<script>" . CloseGB(true) . "</script>";

                redirect(base_url() . 'index.php/?logout');
            }
        } else {
            if ($data['kode'] != "") {
                $resultSet = $this->password_user_model->getPasswordUser($data['kode']);
                $row = $resultSet->row();
                $data['id_user'] = $row->id_user;
                $data['nik'] = $row->nik;
                $data['passworddb'] = $row->password;
                $data['id_group_user'] = $row->id_group_user;
            }
        }

        $this->loadMenuHeaderPopup('management/password_edit', $data);
    }

    public function user_password_reset($id_user = null) {
        $this->load->model('management/password_user_model');

        $data = array();
        $id_user = urldecode($id_user);
        $this->log_event = "Mereset password $id_user";

        $new_password = mt_rand(1000, 1000000);

        $arrVals = array(
            "password" => md5($new_password)
        );

        $this->password_user_model->editpassworduser($id_user, $arrVals);

        $data["sender"] = $this->password_user_model->get_scalar("select nilai from global_setting where kunci='id_modul_interface_textbased'", "nilai");

        $data["no_hp"] = $this->password_user_model->get_scalar("
            SELECT mt_karyawan.nama, mt_karyawan.no_hp
            FROM mt_user
            LEFT JOIN mt_karyawan ON mt_karyawan.nik = mt_user.nik
            LEFT JOIN mt_group_user ON mt_group_user.id_group_user = mt_user.id_group_user WHERE mt_user.id_user = '$id_user'", "no_hp"
        );


        $this->log_event = "Mereset password dari $id_user";

        echo "<script>alert('Password $id_user telah berhasil direset.');</script>";

        echo "<script>" . CloseGB(true) . "</script>";
    }

    public function user_group() {
        $this->load->model("management/user_group");
        $this->load->library(array('greybox', 'datagrid'));

        unset($_SESSION["tool_user_group_from_hak_akses"]);

        $data = array();

        /**
         * Building filter panel
         */
        $field_name = array("a.id_group_user", "a.group_user", "a.keterangan", "a.master_group");
        $field_title = array("ID Group User", "Nama Group", "Keterangan", "ID Master Group");
        $field_type = array("integer", "text", "text", "text");
        $filter_panel = new FilterPanel(base_url() . 'index.php/', $field_name, $field_title, $field_type);

        /**
         * Query data
         */
        $offset = $this->input->get("per_page") ? $this->input->get("per_page") : 0;
        $length = 100;
        $result = $this->user_group->get_data($filter_panel->getFilterString());

        /**
         * Building DataGrid
         */
        $this->datagrid->addMenu('<a href="' . base_url() . 'index.php/management/user_group_export/?' . $this->input->server("QUERY_STRING") . '" class="report" title="Export Data">&nbsp;</a>');
        $this->datagrid->addMenu('<a href="#" class="add" title="Tambah Group User Baru" onclick="add()">&nbsp;</a>');

        $rowCount = $result->num_rows();

        $this->datagrid->setColumns("No.::30", "ID::30", "Nama Group::150", "Keterangan", "Master Group::150");
        $this->datagrid->setPaginationDataNew($rowCount, $length, "management/user_group", $rowCount, $offset);
        
        $this->datagrid->setButtonExportVisible(false);

        if ($rowCount > 0) {
            $nomor = $offset + 1;

            foreach ($result->result() as $row) {
                $rowID = $row->id_group_user;

                $this->datagrid->addRowAction("detil('" . $rowID . "')", "comment", "Detil Data");
                $this->datagrid->addRowAction("edit('" . $rowID . "')", "edit", "Edit Data");
                $this->datagrid->addRowAction("remove('" . $rowID . "')", "delete", "Delete Data");

                $this->datagrid->addRowData($nomor++, $rowID, ucwords($row->group_user), ($row->keterangan == "" ? "Tidak ada keterangan" : $row->keterangan), ($row->master_group != "" ? $row->master_group : "Top Group"));
            }
        } else {
            $this->datagrid->setEmptyDataState("Tidak ada data");
        }

        $data['breadcrumb'] = $this->breadCrumb(true);
        $data['filter_panel'] = $filter_panel;

        $this->log_event = "Membuka menu user group";

        $this->loadMenu("management/user_group", $data);
    }

    public function user_group_master($id = null) {
        $this->load->model("management/user_group");
        $this->load->library("datagrid");

        $data = array();

        /**
         * Building DataGrid
         */
        $this->datagrid->setActionColumnVisible(false);
        $this->datagrid->setColumns("No.", "Nama Group", "Keterangan");
        $this->datagrid->setPaginationData(0, 100, "management/user_group_master", 0, 0);
        $this->datagrid->setMenuVisible(false);

        $modul = $this->user_group->get_data_master_grup($id);

        if ($modul->num_rows() > 0) {
            $nomor = 1;

            foreach ($modul->result() as $row) {
                $this->datagrid->onRowClickAction("getValueSingle('" . $row->id_group_user . "', '" . $row->group_user . "')");

                $this->datagrid->addRowData($nomor++, $row->group_user, $row->keterangan);
            }
        } else {
            $this->datagrid->setEmptyDataState("Tidak ada data");
        }

        $_SESSION["tool_user_group_from_hak_akses"] = true;

        $this->log_event = "Membuka menu User Group Master";

        $this->loadMenuHeaderPopup("management/user_group_master", $data);
    }

    public function user_group_detail($id_group = null) {
        $this->load->model("management/user_group");

        if ($id_group != null) {
            $data = array();

            $data["detail"] = $this->user_group->get_data_by_id($id_group);

            $data["default_hak_akses"] = $this->db->query("SELECT * FROM hak_akses_group WHERE id_group_user = $id_group");

            $data["menus"] = $this->db->query("SELECT a.id_menu, a.id_menu_induk, a.menu, a.link, a.urutan, a.is_active, a.is_popup, b.menu AS menu_group FROM mt_menu a LEFT JOIN mt_menu b ON a.id_menu_induk = b.id_menu WHERE a.id_menu_induk != 0 AND a.is_active = 1 ORDER BY id_menu_induk, menu");

            $this->log_event = "Membuka user group detail dengan id group $id_group";

            $this->loadMenuHeaderPopup("management/user_group_detail", $data);
        } else {
            echo "ID GROUP USER tidak boleh kosong";
        }
    }

    public function user_group_add() {
        $this->load->model("management/user_group");

        if (!isset($_SESSION["tool_user_group_from_hak_akses"])) {
            unset($_SESSION["tool_user_group_default_hak_akses"]);
            unset($_SESSION["tool_user_group_from_hak_akses"]);
            unset($_SESSION["group_user"]);
            unset($_SESSION["keterangan"]);
            unset($_SESSION["master_group"]);
            unset($_SESSION["id_master_group"]);
        }

        $data = array();

        $data["group_user"] = isset($_SESSION["group_user"]) ? $_SESSION["group_user"] : "";
        $data["keterangan"] = isset($_SESSION["keterangan"]) ? $_SESSION["keterangan"] : "";
        $data["master_group"] = isset($_SESSION["master_group"]) ? $_SESSION["master_group"] : "";
        $data["id_master_group"] = isset($_SESSION["id_master_group"]) ? $_SESSION["id_master_group"] : "";

        if ($this->input->post("btnSimpan")) {
            $user_group_insert = array(
                "group_user" => $this->input->post("group_user"),
                "keterangan" => $this->input->post("keterangan"),
                "master_group" => intval($this->input->post("id_master_group")),
            );

            $this->db->insert("mt_group_user", $user_group_insert);

            $id_group_user = $this->user_group->get_scalar("SELECT id_group_user FROM mt_group_user ORDER BY id_group_user DESC LIMIT 1", "id_group_user");

            $default_hak_akses = unserialize($_SESSION["tool_user_group_default_hak_akses"]);

            $id_menus = $this->db->query("SELECT id_menu FROM mt_menu WHERE (is_active = 1 AND id_menu_induk != 0) OR is_popup = 1 ORDER BY id_menu");
            $id_menus = $id_menus->result();

            foreach ($id_menus as $id_menu) {
                if (isset($default_hak_akses[$id_menu->id_menu])) {
                    $this->db->query("INSERT INTO hak_akses_group(id_group_user, id_menu, is_read, is_modify, is_delete) VALUES('$id_group_user', $id_menu->id_menu, " . intval($default_hak_akses[$id_menu->id_menu]["is_read"]) . ", " . intval($default_hak_akses[$id_menu->id_menu]["is_modify"]) . ", " . intval($default_hak_akses[$id_menu->id_menu]["is_delete"]) . ")");
                } else {
                    $this->db->query("INSERT INTO hak_akses_group(id_group_user, id_menu, is_read, is_modify, is_delete) VALUES('$id_group_user', $id_menu->id_menu, 0, 0, 0)");
                }
            }

            $this->log_event = "Menambah user group dengan id $id_group_user";

            echo "<script>alert('Group telah berhasil ditambahkan')</scr" . "ipt>";
            echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
        }

        if (isset($_SESSION["tool_user_group_default_hak_akses"])) {
            $data["default_hak_akses"] = unserialize($_SESSION["tool_user_group_default_hak_akses"]);

            $data["menus"] = $this->db->query("SELECT a.id_menu, a.id_menu_induk, a.menu, a.link, a.urutan, a.is_active, a.is_popup, b.menu AS menu_group FROM mt_menu a LEFT JOIN mt_menu b ON a.id_menu_induk = b.id_menu WHERE a.id_menu_induk != 0 AND a.is_active = 1 ORDER BY id_menu_induk, menu");
        }

        $this->loadMenuHeaderPopup("management/user_group_add", $data);
    }

    public function user_group_edit($id_group = null) {
        $this->load->model("management/user_group");
        $this->load->helper("hak_akses");

        if ($id_group != null) {
            $data = array();

            $data["detail"] = $this->user_group->get_data_by_id($id_group);

            if ($this->input->post("btnSimpan")) {
                $user_group_update = array(
                    "group_user" => $this->input->post("group_user"),
                    "keterangan" => $this->input->post("keterangan"),
                    "master_group" => intval($this->input->post("id_master_group")),
                );

                $this->db->where("id_group_user", $id_group);
                $this->db->update("mt_group_user", $user_group_update);

                $this->log_event = "Mengedit user group dengan id group $id_group";

                echo "<script>alert('Group $user_group_update[group_user] telah berhasil diubah')</scr" . "ipt>";
                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }


            $data["default_hak_akses"] = $this->db->query("SELECT * FROM hak_akses_group WHERE id_group_user = $id_group");

            $data["menus"] = $this->db->query("SELECT a.id_menu, a.id_menu_induk, a.menu, a.link, a.urutan, a.is_active, a.is_popup, b.menu AS menu_group FROM mt_menu a LEFT JOIN mt_menu b ON a.id_menu_induk = b.id_menu WHERE a.id_menu_induk != 0 AND a.is_active = 1 ORDER BY id_menu_induk, menu");

            $this->loadMenuHeaderPopup("management/user_group_edit", $data);
        } else {
            echo "ID GROUP USER tidak boleh kosong";
        }
    }

    public function user_group_hak_akses($type = "edit", $group_user = null, $keterangan = null, $master_group = null, $id_master_group = null) {
        $this->load->model("management/user_group");

        $data = array();

        if ($this->input->post("btnSimpan")) {
            $default_hak_akses = array();

            foreach ($this->input->post("is_read") as $key => $value) {
                $default_hak_akses[$key]["is_read"] = $value;
            }

            foreach ($this->input->post("is_modify") as $key => $value) {
                $default_hak_akses[$key]["is_modify"] = $value;
            }

            foreach ($this->input->post("is_delete") as $key => $value) {
                $default_hak_akses[$key]["is_delete"] = $value;
            }

            if ($type == "edit") {
                $this->db->where("id_group_user", $group_user);
                $this->db->delete("hak_akses_group");

                $id_menus = $this->db->query("SELECT id_menu FROM mt_menu WHERE (is_active = 1 AND id_menu_induk != 0) OR is_popup = 1 ORDER BY id_menu");
                $id_menus = $id_menus->result();

                foreach ($id_menus as $id_menu) {
                    if (isset($default_hak_akses[$id_menu->id_menu])) {
                        $this->db->query("INSERT INTO hak_akses_group(id_group_user, id_menu, is_read, is_modify, is_delete) VALUES('$group_user', $id_menu->id_menu, " . intval($default_hak_akses[$id_menu->id_menu]["is_read"]) . ", " . intval($default_hak_akses[$id_menu->id_menu]["is_modify"]) . ", " . intval($default_hak_akses[$id_menu->id_menu]["is_delete"]) . ")");
                    } else {
                        $this->db->query("INSERT INTO hak_akses_group(id_group_user, id_menu, is_read, is_modify, is_delete) VALUES('$group_user', $id_menu->id_menu, 0, 0, 0)");
                    }
                }

                $this->log_event = "Mengubah hak akses user group $group_user ";
            } else {
                $_SESSION["group_user"] = urldecode($group_user);
                $_SESSION["keterangan"] = urldecode($keterangan);
                $_SESSION["master_group"] = urldecode($master_group);
                $_SESSION["id_master_group"] = urldecode($id_master_group);

                $_SESSION["tool_user_group_default_hak_akses"] = serialize($default_hak_akses);
                $_SESSION["tool_user_group_from_hak_akses"] = true;
            }

            echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
        }

        if ($type == "add") {
            $data["default_hak_akses"] = unserialize($_SESSION["tool_user_group_default_hak_akses"]);
        } else {
            $hak_akses = $this->db->query("SELECT * FROM hak_akses_group WHERE id_group_user = $group_user");

            $data["default_hak_akses"] = array();

            foreach ($hak_akses->result() as $default) {
                $data["default_hak_akses"][$default->id_menu] = array("is_read" => $default->is_read, "is_modify" => $default->is_modify, "is_delete" => $default->is_delete);
            }
        }

        $data["menus"] = $this->db->query("SELECT a.id_menu, a.id_menu_induk, a.menu, a.link, a.urutan, a.is_active, a.is_popup, b.menu AS menu_group FROM mt_menu a LEFT JOIN mt_menu b ON a.id_menu_induk = b.id_menu WHERE a.id_menu_induk != 0 AND a.is_active = 1 ORDER BY id_menu_induk, menu");

        $this->loadMenuHeaderPopup("management/user_group_hak_akses", $data);
    }

    public function user_group_delete($id_group = null) {
        $this->load->model("management/user_group");

        if ($id_group != null) {
            $this->db->where("id_group_user", $id_group);
            $this->db->delete("mt_group_user");

            $this->log_event = "Menghapus group user dengan id $id_group";

            echo "<script>alert('Group dengan ID $id_group telah berhasil dihapus')</scr" . "ipt>";
            echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
        } else {
            echo "ID GROUP USER tidak boleh kosong";
        }
    }

    public function user_group_export() {
        $this->load->model("management/user_group");

        /**
         * Building filter panel
         */
        $field_name = array("a.id_group_user", "a.group_user", "a.keterangan", "a.master_group");
        $field_title = array("ID Group User", "Nama Group", "Keterangan", "ID Master Group");
        $field_type = array("integer", "text", "text", "text");
        $filter_panel = new FilterPanel(base_url() . 'index.php/', $field_name, $field_title, $field_type);

        $query = $this->user_group->get_data_query($filter_panel->getFilterString());

        $this->log_event = "Melakukan export data user group";

        $this->export($query, "Export data group");
    }

    public function user_bm() {
        $this->load->model('management/user_model');

        $data = array();
        $data['breadcrumb'] = $this->breadCrumb(true);
        $data['username'] = $this->userauth->getName();

        $field_name = array("mt_user.id_user", "mt_user.nik", "mt_karyawan.nama", "mt_karyawan.no_hp", "mt_karyawan.email", "mt_group_user.group_user", "mt_user.last_active", "mt_user.is_active");
        $field_title = array("ID Pengguna", "NIK", "Nama Rekan", "No.HP", "Email", "Kelompok Pengguna", "Login Terakhir", "Status");
        $field_type = array("text", "text", "text", "text", "text", "text", "timestamp", "integer");

        $filter_panel = new FilterPanel(base_url() . 'index.php/', $field_name, $field_title, $field_type);
        $whr = $filter_panel->getFilterString();

        $data['fp'] = $filter_panel->drawfilterpanel();
        $data['fp2'] = $filter_panel->drawfilterpanelsingle();

        $data['post'] = $this->getPostInfo();

        $data['resultSetAll'] = $this->user_model->getDataAllUser($whr);

        $data['paging'] = array();
        $data['paging'] = $this->MakePagingPage($data['resultSetAll']['query'], $data['post']['offset'], $data['post']['colPage'], $data['post']['nextPage']);
        $_SESSION[USER_AUTH . 'last_query'] = $data['paging']['result']['lastQuery'];

        $data['resultSet'] = $this->user_model->getDataUser($whr, PAGES_LIMIT, $data['post'] ['offset']);
        $data['result'] = $data['resultSet']->result();
        $data['numRows'] = $data['resultSet']->num_rows();



        $this->log_event = "Membuka menu data pengguna";

        $this->loadMenu('management/user_bm', $data);
    }

    public function user_bm_activity_count() {
        $this->load->model('management/user_model');

        $data = array();

        $this->log_event = "Membuka aktifitas terkini data pengguna";

        $this->loadMenu('management/user_bm', $data);
    }

    public function user_bm_delete($kode) {
        $this->load->model('management/user_model');

        $kode = str_replace("%20", " ", $kode);

        if ($kode != null) {
            $this->user_model->removeDataUser($kode);

            $this->db->query("DELETE FROM hak_akses WHERE id_user = '$kode'");

            $this->log_event = "Menghapus Data User dengan ID User " . $kode;

            redirect(base_url() . 'index.php/management/user_bm', 'refresh');
        }
    }

    function user_bm_hak_akses($id = null) {
        $this->load->model("management/user_group");

        $id = urldecode($id);

        if ($id != null) {
            $data = array();

            if ($this->input->post("btnSimpan")) {
                $hak_akses = $this->input->post("hak_akses");

                $this->db->query("DELETE FROM hak_akses WHERE id_user = '$id'");

                $id_menus = $this->db->query("SELECT id_menu FROM mt_menu WHERE is_active = 1 AND id_menu_induk != 0 ORDER BY id_menu");
                $id_menus = $id_menus->result();

                foreach ($id_menus as $id_menu) {
                    if (isset($hak_akses[$id_menu->id_menu])) {
                        $this->db->query("INSERT INTO hak_akses(id_user, id_menu, is_read, is_modify, is_delete) VALUES('$id', $id_menu->id_menu, " . intval($hak_akses[$id_menu->id_menu]["read"]) . ", " . intval($hak_akses[$id_menu->id_menu]["edit"]) . ", " . intval($hak_akses[$id_menu->id_menu]["delete"]) . ")");
                    } else {
                        $this->db->query("INSERT INTO hak_akses(id_user, id_menu, is_read, is_modify, is_delete) VALUES('$id', $id_menu->id_menu, 0, 0, 0)");
                    }
                }

                $this->log_event = "Mengubah hak akses user " . $id;

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }

            $data["user"] = $this->db->query("SELECT * FROM mt_user WHERE id_user = '$id'");

            if ($data["user"]->num_rows() <= 0) {
                echo "ID user tidak ditemukan";
                exit;
            }

            $data["hak_akses"] = $this->db->query("SELECT * FROM hak_akses WHERE id_user = '$id'");

            $data["menus"] = $this->db->query("SELECT a.id_menu, a.id_menu_induk, a.menu, a.link, a.urutan, a.is_active, a.is_popup, b.menu AS menu_group FROM mt_menu a LEFT JOIN mt_menu b ON a.id_menu_induk = b.id_menu WHERE a.id_menu_induk != 0 AND a.is_active = 1 ORDER BY id_menu_induk, menu");

            $data["menus_final"] = array();

            $counter = 0;

            foreach ($data["menus"]->result() as $menu) {
                $row = array();
                $row["id_menu"] = $menu->id_menu;
                $row["menu"] = $menu->menu;
                $row["menu_group"] = $menu->menu_group;

                foreach ($data["hak_akses"]->result() as $akses) {
                    if ($menu->id_menu == $akses->id_menu) {
                        $row["is_read"] = $akses->is_read;
                        $row["is_modify"] = $akses->is_modify;
                        $row["is_delete"] = $akses->is_delete;

                        break;
                    } else {
                        $row["is_read"] = 0;
                        $row["is_modify"] = 0;
                        $row["is_delete"] = 0;
                    }
                }

                $data["menus_final"][] = $row;

                $counter++;
            }

            $this->loadMenuHeaderPopup("management/user_bm_hak_akses", $data);
        } else {
            echo "ID user tidak boleh kosong";
        }
    }

    public function user_bm_setAktif($kode, $status) {
        $this->load->model('management/user_model');

        $kode = str_replace("%20", " ", $kode);

        if ($kode != null) {
            $status_aktif = $status == "0" ? "1" : "0";
            $info = $status == "0" ? "Mengaktifkan" : "Menonaktifkan";

            $arrVals = array(
                "is_active" => $status_aktif
            );

            $this->user_model->setActiveDataUser($kode, $arrVals);

            $this->log_event = $info . " User dengan ID User " . $kode;

            redirect(base_url() . 'index.php/management/user_bm', 'refresh');
        }
    }

    public function user_bm_export() {
        $this->log_event = "Melakukan export data pengguna";
        
        $this->export($_SESSION[USER_AUTH . 'last_query']);
    }

    public function user_bm_add() {
        $this->load->model('management/user_add_model');

        $data = array();

        $data['id_user'] = $this->input->get_post('id_user');
        $data['nik'] = $this->input->get_post('nik');
        $data['nama'] = $this->input->get_post('nama');
        $data['password'] = $this->input->get_post('password');
        $data['password2'] = $this->input->get_post('password2');
        $data['id_group_user'] = $this->input->get_post('id_group_user');
        $data['group_user'] = $this->input->get_post('group_user');
        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['id_user'] == "") {
                $data['msg'] = "Parameter ID Pengguna tidak ditemukan!";
            } else if ($data['password'] == "") {
                $data['msg'] = "Parameter Password Pengguna tidak ditemukan!";
            } else if ($data['password2'] == "") {
                $data['msg'] = "Parameter Confirm Password tidak ditemukan!";
            } else if ($data['password2'] != $data['password'] ) {
                $data['msg'] = "Konfirmasi Password tidak sama!";
            } else if ($this->general_model->checkDupKey("id_user", $data['id_user'], "mt_user") && $data['id_user'] != $data['kode']) {
                $data['msg'] = "ID Pengguna " . $data['id_user'] . " sudah terdaftar, silakan mengisi dengan ID yang berbeda !";
            } else if ($data['nik'] == "") {
                $data['msg'] = "Parameter NIK tidak ditemukan!";
            } else if ($data['id_group_user'] == "") {
                $data['msg'] = "Parameter Kelompok Pengguna tidak ditemukan!";
            } else {
                $arrVals = array(
                    "id_user" => $data['id_user'],
                    "nik" => $data['nik'],
                    "id_group_user" => $data['id_group_user'],
                    "password" => md5($data['password'])
                );

                if ($data['vMode'] == "add") {
                    $this->db->query("
                            INSERT INTO hak_akses  
                            SELECT '$data[id_user]', id_menu, is_read, is_modify, is_delete FROM hak_akses_group WHERE id_group_user = $data[id_group_user]
                        ");

                    $this->user_add_model->addDataUser($arrVals);

                    $this->log_event = "Menambah Data User dengan ID User " . $data['id_user'];
                }

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }
        }

        $data['gbkaryawan'] = $this->greybox->createLink(base_url() . "index.php/popup/karyawan", "<span>Cari</span>", "Cari Data Rekan", "find", "", 750, 450);
        $data['gbkeluser'] = $this->greybox->createLink(base_url() . "index.php/popup/kelompok_user", "<span>Cari</span>", "Cari Kelompok Pengguna", "find", "", 750, 450);

        $this->loadMenuHeaderPopup('management/user_bm_add', $data);
    }

    public function user_bm_edit($kode) {

        $this->load->model('management/user_edit_model');
        $this->load->model('management/user_add_model');

        $data = array();

        $data['kode'] = str_replace("%20", " ", $kode);
        $data['id_user'] = $this->input->get_post('id_user');
        $data['nik'] = $this->input->get_post('nik');
        $data['nama'] = $this->input->get_post('nama');
        $data['password'] = $this->input->get_post('password');
        $data['password2'] = $this->input->get_post('password2');
        $data['id_group_user'] = $this->input->get_post('id_group_user');
        $data['group_user'] = $this->input->get_post('group_user');
        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['id_user'] == "") {
                $data['msg'] = "Parameter ID Pengguna tidak ditemukan!";
            } else if ($data['password2'] != $data['password'] ) {
                $data['msg'] = "Konfirmasi Password tidak sama!";
            } else if ($this->general_model->checkDupKey("id_user", $data['id_user'], "mt_user") && $data['id_user'] != $data['kode']) {
                $data['msg'] = "ID Pengguna " . $data['id_user'] . " sudah terdaftar, silakan mengisi dengan ID yang berbeda !";
            } else if ($data['nik'] == "") {
                $data['msg'] = "Parameter NIK tidak ditemukan!";
            } else if ($data['id_group_user'] == "") {
                $data['msg'] = "Parameter Kelompok Pengguna tidak ditemukan!";
            } else {
                $arrFields = array("id_user", "nik", "id_group_user", "password");
                $arrVals = array($data['id_user'], $data['nik'], $data['id_group_user'], md5("1234567"));

                if ($data['vMode'] == "add") {
                    $this->user_add_model->addDataUser($arrFields, $arrVals);
                } else {
                    $data['id_group'] = $this->general_model->getScalar('id_group_user', 'mt_user', 'id_user', $data['id_user']);

                    if ($data['id_group_user'] != $data['id_group']) {
                        $this->db->query("DELETE FROM hak_akses WHERE id_user = '$kode'");

                    }
                    if($data['password'] == ""){
                        $arrVals = array(
                        "id_user" => $data['id_user'],
                        "nik" => $data['nik'],
                        "id_group_user" => $data['id_group_user']
                    );
                    }else{
                        $arrVals = array(
                        "id_user" => $data['id_user'],
                        "nik" => $data['nik'],
                        "id_group_user" => $data['id_group_user'],
                        "password" => md5($data['password'])
                    );
                    }
                    
                    $this->user_edit_model->updateDataUSer($data['kode'], $arrVals);

                    $this->log_event = "Mengubah Data User dengan ID User " . $data['kode'];
                }

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }
        } else {
            if ($data['kode'] != "") {
                $resultSet = $this->user_edit_model->getDataUser($data['kode']);
                $result = $resultSet->row();
                $data['id_user'] = $result->id_user;
                $data['nik'] = $result->nik;
                $data['id_group_user'] = $result->id_group_user;
                $data['nama'] = $this->general_model->getScalar('nama', 'mt_karyawan', 'nik', $data['nik']);
                $data['group_user'] = $this->general_model->getScalar('group_user', 'mt_group_user', 'id_group_user', intval($data['id_group_user']));
            }
        }

        $data['gbkaryawan'] = $this->greybox->createLink(base_url() . "index.php/popup/karyawan", "<span>Cari</span>", "Cari Data Rekan", "find", "", 750, 450);
        $data['gbkeluser'] = $this->greybox->createLink(base_url() . "index.php/popup/kelompok_user", "<span>Cari</span>", "Cari Kelompok Pengguna", "find", "", 750, 450);

        $this->loadMenuHeaderPopup('management/user_bm_add', $data);
    }
    
    public function divisi() {
        $this->load->model('management/divisi');

        $data = array();
        $data['breadcrumb'] = $this->breadCrumb(true);
        
        $field_name = array("id_divisi", "divisi", "divisi_sname");
        $field_title = array("ID Divisi", "Nama Divisi", "Inisial");
        $field_type = array("integer", "text", "text");

        $filter_panel = new FilterPanel(base_url() . 'index.php/', $field_name, $field_title, $field_type);
        $whr = $filter_panel->getFilterString();

        $data['fp'] = $filter_panel->drawfilterpanel();
        $data['fp2'] = $filter_panel->drawfilterpanelsingle();

        $data['post'] = $this->getPostInfo();

        $data['resultSetAll'] = $this->divisi->getDataAllDivisi($whr);

        $data['resultSet'] = $this->divisi->getDataDivisi($whr, PAGES_LIMIT, $data['post'] ['offset']);
        $data['result'] = $data['resultSet']->result();
        $data['numRows'] = $data['resultSet']->num_rows();

        $this->log_event = "Membuka menu data divisi";

        $this->loadMenu('management/divisi', $data);
    }
    
    public function divisi_edit($kode) {

        $this->load->model('management/divisi');
        
        $data = array();

        $data['kode'] = str_replace("%20", " ", $kode);
        $data['divisi'] = $this->input->get_post('divisi');
        $data['divisi_sname'] = $this->input->get_post('divisi_sname');
        
        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['divisi'] == "") {
                $data['msg'] = "Parameter Divisi tidak ditemukan!";
            } else if ($data['divisi_sname'] == "") {
                $data['msg'] = "Parameter Inisial tidak ditemukan!";
            
            } else {
                $arrFields = array("divisi", "divisi_sname");
                $arrVals = array($data['divisi'], $data['divisi_sname']);

                if ($data['vMode'] == "add") {

                    $this->db->query("
                            INSERT INTO mt_divisi (divisi,divisi_sname,divisi_induk)  
                            values ('$data[divisi]','$data[divisi_sname]',1)");
                } else {
                    $arrVals = array(
                        "divisi" => $data['divisi'],
                        "divisi_sname" => $data['divisi_sname']
                    );
                    $this->divisi->updateDataDivisi($data['kode'], $arrVals);

                    $this->log_event = "Mengubah Data Divisi dengan ID Divisi " . $data['kode'];
                }

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }
        } else {
            if ($data['kode'] != "") {
                $resultSet = $this->divisi->getDataSingleDivisi($data['kode']);
                $result = $resultSet->row();
                $data['id_divisi'] = $result->id_divisi;
                $data['divisi'] = $result->divisi;
                $data['divisi_sname'] = $result->divisi_sname;
                
            }
        }

        
        $this->loadMenuHeaderPopup('management/divisi_edit', $data);
    }
    
    public function divisi_add() {
        $this->load->model('management/divisi');

        $data = array();

        $data['divisi'] = $this->input->get_post('divisi');
        $data['divisi_sname'] = $this->input->get_post('divisi_sname');
        $data['vSave'] = $this->input->get_post('vSave');
        $data['vMode'] = $this->input->get_post('vMode');
        $data['msg'] = "";

        if ($data['vSave']) {
            if ($data['divisi'] == "") {
                $data['msg'] = "Parameter Divisi tidak ditemukan!";
            } else if ($data['divisi_sname'] == "") {
                $data['msg'] = "Parameter Inisial tidak ditemukan!";
            } else {
                $arrVals = array(
                    "divisi" => $data['divisi'],
                    "divisi_sname" => $data['divisi_sname'],
                );

                if ($data['vMode'] == "add") {
                    $this->db->query("
                            INSERT INTO mt_divisi (divisi,divisi_sname,divisi_induk)  
                            values ('$data[divisi]','$data[divisi_sname]',1)
                        ");



                    $this->log_event = "Menambah Data Divisi";
                }

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }
        }

        $this->loadMenuHeaderPopup('management/divisi_edit', $data);
    }
    
    public function divisi_delete($kode) {
        $this->load->model('management/divisi');

        $kode = str_replace("%20", " ", $kode);

        if ($kode != null) {
//            $this->user_model->removeDataUser($kode);

            $this->db->query("DELETE FROM mt_divisi WHERE id_divisi = '$kode'");

            $this->log_event = "Menghapus Data Divisi dengan ID Divsi " . $kode;

            redirect(base_url() . 'index.php/management/divisi', 'refresh');
        }
    }
    
    function nama_hari($tgl)
    {
        $tanggal = strtotime($tgl);
        $hari_en = date('l', $tanggal);
        $hari_ar = array("Monday"=>"SENIN", "Tuesday"=>"SELASA", "Wednesday"=>"RABU", "Thursday"=>"KAMIS", "Friday"=>"JUM'AT", "Saturday"=>"SABTU", "Sunday"=>"MINGGU");
        $hari_id = $hari_ar[$hari_en];
        return $hari_id;
    }
}
